//
//  AgentDetailViewController.m
//  EPBPro
//
//  Created by wucha on 2018/7/10.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "AgentDetailViewController.h"
#import "HttpOrgAction.h"
#import "ShopTypeTableViewCell.h"
#import "MerchantContractInfo.h"
#import "ShopTypeDetailViewController.h"
#import "OrgContractListViewController.h"
#import "OrgGainInfo.h"
#import "KnotViewController.h"
#import "HttpContractAction.h"

@interface AgentDetailViewController ()
@property (nonatomic,strong) OrgInfo *orgInfo;
@property (nonatomic,strong) NSArray *listArray;
@property (nonatomic,strong) UILabel *integralLabel;

@end

@implementation AgentDetailViewController

-(id)initWithOrgInfo:(OrgInfo*)orgInfo{
    if (self == [super init]) {
        self.orgInfo = orgInfo;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIButton *rightbutton  = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 44)];
    rightbutton.titleLabel.font = [UIFont systemFontOfSize:14];
    [rightbutton setTitle:@"查看合同" forState:UIControlStateNormal];
    [rightbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightbutton addTarget:self action:@selector(contractAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightbutton];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    self.tableView.separatorStyle = 0;
    self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-44);
    self.tableView.tableHeaderView = [self getHeaderView];
    
    UIButton *knotButton = [UIButton new];
    [knotButton setTitle:@"结款" forState:UIControlStateNormal];
    [knotButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [knotButton addTarget:self action:@selector(knotAction:) forControlEvents:UIControlEventTouchUpInside];
    knotButton.backgroundColor = DefaultRedColor;
    [self.view addSubview:knotButton];
    knotButton.sd_layout.leftSpaceToView(self.view, 0).bottomSpaceToView(self.view, 0).widthIs(SCREEN_WIDTH).heightIs(44);
    [self loadMachineNumber];
    [self loadData];
}

-(void)knotAction:(id)sender{
    KnotViewController *knotViewController = [[KnotViewController alloc] initWithOrgId:self.orgInfo.id block:^{
        [self loadData];
    }];
    [self.navigationController pushViewController:knotViewController animated:YES];
}

-(void)loadData{
    [self showWaitingViewWithMessage:@"加载中..." style:WaitingViewShowStyleFullSelfView];
    [HttpOrgAction getOrgGainList:self.orgInfo.id block:^(id result, NSError *error) {
        [self dismissWaitingView];
        if (!error) {
            self.listArray = result;
            [self.tableView reloadData];
            
            if (self.listArray.count == 0) {
                [self showNoResultWithFrame:CGRectMake(0, 290, SCREEN_WIDTH, SCREEN_HEIGHT-290-44)];
            }else{
                [self hiddenNoResultView];
            }
        }
    }];
}

-(void)contractAction:(id)sender{
    OrgContractListViewController *orgContractListViewController = [[OrgContractListViewController alloc] initWithOrgInfo:self.orgInfo];
    [self.navigationController pushViewController:orgContractListViewController animated:YES];
}

-(void)loadMachineNumber{
    NSDictionary *params = @{@"orgId":self.orgInfo.id};
    [HttpContractAction getContractTotalInfo:params block:^(id result, NSError *error) {
        if (!error) {
            NSDictionary *dic = result;
            self.integralLabel.text = [NSString stringWithFormat:@"机器数量\n%d",[dic[@"totalEquipmentNumber"] intValue]];
        }
    }];
}

-(UIView*)getHeaderView{
    UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 290)];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250)];
    view.backgroundColor = DefaultBlueColor;
    [bView addSubview:view];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.font = [UIFont systemFontOfSize:20];
    titleLabel.text = self.orgInfo.name;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    [view addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(view, 0).topSpaceToView(view, StatusBarHeight+44).widthIs(SCREEN_WIDTH).heightIs(20);
    
    UIButton *addressButton = [UIButton new];
    addressButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [addressButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [addressButton setTitle:[NSString stringWithFormat:@"%@%@%@%@",self.orgInfo.province?self.orgInfo.province:@"",self.orgInfo.city,self.orgInfo.area,self.orgInfo.address] forState:UIControlStateNormal];
    [addressButton setImage:[UIImage imageNamed:@"machine_local"] forState:UIControlStateNormal];
    [view addSubview:addressButton];
    addressButton.sd_layout.leftSpaceToView(view, 20).topSpaceToView(titleLabel, 10).widthIs(SCREEN_WIDTH-40).heightIs(20);
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = [UIColor whiteColor];
    [view addSubview:lineView];
    lineView.sd_layout.leftSpaceToView(view, 0).bottomSpaceToView(view, 75).rightSpaceToView(view, 0).heightIs(1);
    
    UILabel *totalEarnLabel = [UILabel new];
    totalEarnLabel.text = [NSString stringWithFormat:@"总收益\n%.2f",[Utils getFloatFromString:self.orgInfo.profit]];
    totalEarnLabel.textColor = [UIColor whiteColor];
    totalEarnLabel.textAlignment = NSTextAlignmentCenter;
    totalEarnLabel.numberOfLines = 0;
    totalEarnLabel.font = [UIFont systemFontOfSize:14];
    [view addSubview:totalEarnLabel];
    totalEarnLabel.sd_layout.leftSpaceToView(view, 0).bottomSpaceToView(view, 0).heightIs(74).widthIs(SCREEN_WIDTH/2);
    
    UILabel *integralLabel = [UILabel new];
    integralLabel.text = @"机器数量\n3000";
    integralLabel.textColor = [UIColor whiteColor];
    integralLabel.textAlignment = NSTextAlignmentCenter;
    integralLabel.numberOfLines = 0;
    integralLabel.font = [UIFont systemFontOfSize:14];
    [view addSubview:integralLabel];
    integralLabel.sd_layout.leftSpaceToView(totalEarnLabel, 0).bottomSpaceToView(view, 0).heightIs(74).widthIs(SCREEN_WIDTH/2);
    self.integralLabel = integralLabel;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 250, SCREEN_WIDTH-20, 40)];
    label.font = [UIFont systemFontOfSize:14];
    label.text = @"交易记录";
    [bView addSubview:label];
    return bView;
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
        UIView *backView = [UIView new];
        backView.backgroundColor = DefaultBlueColor;
        [cell.contentView addSubview:backView];
        backView.sd_layout.leftSpaceToView(cell.contentView, 0).topSpaceToView(cell.contentView, 0).rightSpaceToView(cell.contentView, 0).heightIs(20);
        
        UILabel *titleLabel = [UILabel new];
        titleLabel.tag = 10;
        titleLabel.font = [UIFont systemFontOfSize:14];
        titleLabel.textColor = [UIColor whiteColor];
        [backView addSubview:titleLabel];
        titleLabel.sd_layout.leftSpaceToView(backView, 20).topSpaceToView(backView, 0).heightIs(20).autoWidthRatio(0);
        [titleLabel setSingleLineAutoResizeWithMaxWidth:200];
        
        UILabel *moneyLabel = [UILabel new];
        moneyLabel.tag = 11;
        moneyLabel.font = [UIFont systemFontOfSize:14];
        moneyLabel.textColor = [UIColor whiteColor];
        moneyLabel.textAlignment = NSTextAlignmentRight;
        [backView addSubview:moneyLabel];
        moneyLabel.sd_layout.rightSpaceToView(backView, 20).topSpaceToView(backView, 0).heightIs(20).autoWidthRatio(0);
        [moneyLabel setSingleLineAutoResizeWithMaxWidth:200];
        
    }
    // Configure the cell...
    OrgGainInfo *orgGainInfo = self.listArray[indexPath.row];
    UILabel *titleLabel = [cell.contentView viewWithTag:10];
    titleLabel.text = orgGainInfo.createTime;
    
    UILabel *moneyLabel = [cell.contentView viewWithTag:11];
    moneyLabel.text = [NSString stringWithFormat:@"%@%.2f元",orgGainInfo.txnType==0?@"-":@"+",orgGainInfo.amount];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
